﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.Entity;
using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Util;
using ComponentFactory.Krypton.Toolkit;
using NHibernate;

namespace BotonJXC.Forms.Controls
{
    public partial class DepartmentControl : BaseInfoControl
    {
        DepartmentBLL _depBll = new DepartmentBLL();
        StoreBLL _storeBll = new StoreBLL();
        IList<Department> _departments = null;
        string _depName = null;
        int _storeId = 0;
        short _depLevel = 0;
        IList<Store> _stores = null;
        IList<ComboBoxDropItem> _depLevels = OrgLevelUtil.CreateOrgLevelList();
        public DepartmentControl()
        {
            InitializeComponent();
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvDepartment.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {

        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvDepartment.Rows)
                {
                    Department dep = r.DataBoundItem as Department;
                    if (dep == null)
                        continue;
                    if (dep.DepId == 0)
                    {
                        _depBll.Save(dep);
                    }
                    else if ("1".Equals(r.Tag) && dep.DepId != 0)
                    {
                        _depBll.Update(dep);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void DeleteInfo()
        {
            IList<Department> units = (IList<Department>)kdgvDepartment.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvDepartment.SelectedRows)
                {
                    Department unit = r.DataBoundItem as Department;
                    if (unit != null)
                    {
                        _storeBll.Delete(unit.DepId);
                    }
                    units.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvDepartment.Refresh();
        }

        private void DepartmentControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                LoadStoreInfo();
                LoadLevelInfo();
                LoadData();

            }
        }

        void LoadStoreInfo()
        {
            _stores = _storeBll.FindAll();
            kcbStoreId.DataSource = new BindingList<Store>(_stores);
            foreach (Store s in _stores)
            {
                kdgvcbcStoreName.Items.Add(s.StoreName);
            }
        }

        void LoadLevelInfo()
        {
            kcbDepLevel.DataSource = new BindingList<ComboBoxDropItem>(_depLevels);
            foreach (ComboBoxDropItem c in _depLevels)
            {
                kdgvcbcDepLevel.Items.Add(c.Text);
            }
        }

        void LoadData()
        {
            PageModel<Department> pageModel = _depBll.Find(_depName, _storeId, _depLevel, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            _departments = pageModel.Results;
            kdgvDepartment.DataSource = new BindingList<Department>(_departments);
        }

        private void kdgvDepartment_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2 && _departments.Count > e.RowIndex)
            {
                Department dep = _departments[e.RowIndex];
                e.Value = dep.DepLevel + "级";
            } if (e.ColumnIndex == 1 && _departments.Count > e.RowIndex)
            {
                Department dep = _departments[e.RowIndex];
                KryptonDataGridViewComboBoxCell cbc = (KryptonDataGridViewComboBoxCell)kdgvDepartment.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (dep.Store != null)
                {
                    cbc.Value = dep.Store.StoreName;
                }
            }
        }

        private void kdgvDepartment_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvDepartment.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvDepartment_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (kdgvDepartment.CurrentCell.ColumnIndex == 1 || kdgvDepartment.CurrentCell.ColumnIndex == 2)
            {
                KryptonDataGridViewComboBoxEditingControl ec = e.Control as KryptonDataGridViewComboBoxEditingControl;
                ec.DropDownClosed += new EventHandler(ec_DropDownClosed);
            }               
        }

        void ec_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl ec = (KryptonDataGridViewComboBoxEditingControl)sender;
            if (kdgvDepartment.CurrentCell.ColumnIndex == 1 && ec.SelectedIndex > -1)
            {
                Department dep = _departments[kdgvDepartment.CurrentCell.RowIndex];
                dep.Store = _stores[ec.SelectedIndex];
            }
            else if (kdgvDepartment.CurrentCell.ColumnIndex == 2 && ec.SelectedIndex > -1)
            {
                Department dep = _departments[kdgvDepartment.CurrentCell.RowIndex];
                dep.DepLevel = (short)_depLevels[ec.SelectedIndex].Value;
            }
        }

        private void kdgvDepartment_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _depName = ktbDepName.Text.Trim();
            _depLevel = (short)(kcbDepLevel.SelectedIndex+1);
            _storeId = (int)kcbStoreId.SelectedValue;
            LoadData();
        }
    }
}
